// appsplus_jinglian/pages/order_record/order_record.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabindex:0,
    ticket_status:{
      '0':'未支付',
      '1':"已付款待处理",
      '2':'已取票',
      '3': '已退款',
      '4':'已取消'
    },
    page:1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // get_order_list(this)
    this.getorder()
    var that = this;
    wx.getStorage({
      key: 'jl_user',
      success: function (res) {
        console.log(res)
        that.setData({
          user: res.data
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page:1,
    })
    this.getorder()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getorder()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  order_cancel:function(e){
    var that = this;
    console.log(e)
    var token = this.data.user.token;
    var out_trade_no = e.currentTarget.dataset.orderid
    app.util.request({
      'url':"entry/wxapp/revocation_order",
      data:{
        token: token,
        out_trade_no: out_trade_no,
      },
      success:function(res){
        var ret = res.data;
        if(ret.status){
          app.util.message(ret.msg)
          that.onLoad()
        }
        console.log(res)
      }
    })
  },
  // 重新支付
  re_pay:function(e){
    var that = this;
    var token = this.data.user.token;
    var out_trade_no = e.currentTarget.dataset.orderid;
        app.util.request({
          url:"entry/wxapp/orders_payment",
          data:{
            token: token,
            out_trade_no: out_trade_no
          },
          success:function(res){
            console.log(res)
            var ret =res.data;
            if(ret.status){
              wx.requestPayment({
                timeStamp: ret.data.timeStamp + '',
                nonceStr: ret.data.nonceStr + '',
                package: ret.data.package,
                signType: ret.data.signType,
                paySign: ret.data.paySign,
                success: function (res) {
                  that.pay_finish(out_trade_no)
                  that.onLoad()
                },
                fail: function (res) {
                }
              })
            }
          }
        })
      },
  order_detail:function(e){
    console.log(e)
      wx.navigateTo({
        url: '../order_detail/order_detail?data=' + JSON.stringify(e.currentTarget.dataset.info),
      })
  },
  // 获取订单列表
  getorder:function(e){
    var that = this
    console.log(e)
    if(e){
      this.setData({
        tabindex: e.currentTarget.dataset.type,
        page:1,
      })
    }

    try {
      var value = wx.getStorageSync('jl_user')
      if (value) {
        // Do something with return value
        var page = that.data.page
        app.util.request({
          url: 'entry/wxapp/orders_list',
          data: { token: value.token, state: that.data.tabindex, page: page},
          success: function (ret) {
            wx.stopPullDownRefresh()
            console.log(ret)
            var res = ret.data;
            if (res.status){
              if (res.data.length>0){
                that.setData({
                  page:page+1
                })
              }
              if(page>1){
                var list = that.data.order_list;
                list = list.concat(res.data);
                that.setData({
                  order_list: list
                })
                if (res.data.length==0){
                  that.setData({
                    btm_line: true,
                  })
                  setTimeout(function () {
                    that.setData({
                      btm_line: false,
                    })
                  }, 2000)
                  app.util.message(res.data.msg, '', 'err_msg')
                }
              }else{
                that.setData({
                  order_list: res.data
                })
              }
              
            }else{
              if(page==1){
                that.setData({
                  order_list: []
                })
              }
              
            }  
          }
        })
      }
    } catch (e) {
      // Do something when catch error
      wx.showToast({
        title: '请先登录！',
      })
    }
  },
  detail:function(e){
    var order_id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../order_detail/order_detail?order_id=' + order_id,
    })
  },
  pay_finish: function (out_trade_no) {
    app.util.request({
      url: "entry/wxapp/orders_with",
      data: {
        token: wx.getStorageSync('jl_user').token,
        out_trade_no: out_trade_no
      },
      success: function (ret) {

      }
    })
  }
});





function get_order_list(that) {

  try {
    var value = wx.getStorageSync('jl_user')
    if (value) {
      // Do something with return value
      app.util.request({
        url: 'entry/wxapp/orders_list',
        data: { token: value.token },
        success: function (ret) {
          console.log(ret)
          var res = ret.data;
          if(res.status)
          that.setData({
            order_list:res.data
          })
        }
      })
    }
  } catch (e) {
    // Do something when catch error
    wx.showToast({
      title: '请先登录！',
    })
  }



}